package com.ijmacd.footballtracker;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TeamViewActivity extends Activity {
	private static final String TAG = "TeamViewActivity";
	
	/**
     * The columns we are interested in from the database
     */
    private static final String[] PROJECTION = new String[] {
            Provider.Teams._ID, // 0
            Provider.Teams.NAME, // 1
            Provider.Teams.LOGO_SMALL, // 2
    };
	private static final int COLUMN_INDEX_UUID = 0;
	private static final int COLUMN_INDEX_NAME = 1;
	private static final int COLUMN_INDEX_LOGO = 2;

	private static final int MENU_ITEM_VIEWMATCHES = 0;
	
	private Uri mUri;
	private TextView mNameText;
	private ImageView mLogoImage;
	private Cursor mCursor;

	private String mUuid;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        final Intent intent = getIntent();

        mUri = intent.getData();
		
		setContentView(R.layout.teamview);

		mNameText = (TextView)findViewById(R.id.team_name_txt);
		mLogoImage = (ImageView)findViewById(R.id.logo_img);
		
		mCursor = managedQuery(mUri, PROJECTION, null, null, null);
	}
    @Override
    protected void onResume() {
        super.onResume();

        // If we didn't have any trouble retrieving the data, it is now
        // time to get at the stuff.
        if (mCursor != null && mCursor.getCount() > 0) {
            // Make sure we are at the one and only row in the cursor.
            mCursor.moveToFirst();
            
            mUuid = mCursor.getString(COLUMN_INDEX_UUID);

            String name = mCursor.getString(COLUMN_INDEX_NAME);
            mNameText.setText(name);
            
            String logo = mCursor.getString(COLUMN_INDEX_LOGO);
            if(logo != null){
            	mLogoImage.setImageURI(Uri.parse(logo));
            }
            
        } else {
            mNameText.setText(getText(R.string.error_team_notfound));
        }
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, MENU_ITEM_VIEWMATCHES, 0, R.string.view_matches);
		
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_ITEM_VIEWMATCHES:
        	startActivity(new Intent(Intent.ACTION_VIEW,
        			Uri.parse(Provider.Matches.CONTENT_URI +  "?team=" + mUuid)));
            return true;
        }
        return super.onOptionsItemSelected(item);
	}
}
